<?php
/**
 * Created by PhpStorm.
 * User: php
 * Date: 2016/8/22
 * Time: 16:16
 */

namespace App;


class Help
{
    public static function get_file_postfix($file)
    {
        $over_name = explode('.', $file);
        return $over_name[count($over_name) - 1];
    }

    public static function get_file_type($file)
    {
        $over_name = explode('.', $file);
        $over_name =  $over_name[count($over_name) - 1];

        $pattern_video = "/(swf|flv|mp4|rmvb|avi|mpeg|ram|mov|wmv)/i";
        $pattern_music = "/(mp3|wav|wma|ogg|ape|acc)/i";
        $pattern_img = "/(gif|jpg|jpeg|bmp|png)/i";
        $pattern_bt = "/torrent/i";
        if (preg_match($pattern_video, $over_name) == 1) {
            return 'video';
        }elseif(preg_match($pattern_music, $over_name) == 1){
            return 'music';
        }elseif(preg_match($pattern_img, $over_name) == 1){
            return 'img';
        }elseif(preg_match($pattern_bt, $over_name) == 1){
            return 'bt';
        }else{
            return 'file';
        }
    }
    public static function formatBytes($size) {
        $units = array(' B', ' KB', ' MB', ' GB', ' TB');
        for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
        return round($size, 2).$units[$i];
    }
}